//
//  XFYRadioView.m
//  MyWallet
//
//  Created by 小肥羊 on 15/8/24.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "XFYRadioView.h"

@implementation XFYRadioView
#pragma mark - 初始化
- (void)initValue
{
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initValue];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initValue];
    }
    return self;
}

#pragma mark - Set设置属性
- (void)setContents:(NSMutableArray *)contents
{
    _contents = contents;
    [self upViews];
}

- (void)upViews
{
    for(UIView *contentView in [self subviews])
    {
        [contentView removeFromSuperview];
    }
    float itemWidth = self.frame.size.width/self.contents.count;
    float itemHeight = self.frame.size.height;
    for (NSInteger i=0; i<self.contents.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTag:ViewTag(i)];
        [button setFrame:(CGRect){CGPointMake(itemWidth * i, 0), CGSizeMake(itemWidth, itemHeight)}];
        [button setTitle:self.contents[i] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        if (i == 0) {
            [button setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateNormal];
        } else {
            [button setImage:[UIImage imageNamed:@"NotSelected"] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(operation:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self upViews];
}


#pragma mark - 点击按钮
- (void)operation:(UIButton *)button
{
    for(UIButton *btn in [self subviews])
    {
        [btn setImage:[UIImage imageNamed:@"NotSelected"] forState:UIControlStateNormal];
    }
    [button setImage:[UIImage imageNamed:@"Selected"] forState:UIControlStateNormal];
    if (self.delegate) {
        [self.delegate operation:self Button:button AtIndex:ArrayIndex(button.tag)];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
